/** 
 * Project Name:eleme-website 
 * File Name:ShopCartDao.java 
 * Package Name:com.eleme.dao.impl 
 * Date:2017年12月28日下午12:18:11 
 * Copyright (c) 2017, fakedesigner@163.com All Rights Reserved. 
 * 
*/  
  
package com.eleme.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.eleme.dao.IShopCartDao;
import com.eleme.entity.ShopCart;
import com.eleme.entity.ShopCartDtl;

/** 
 * ClassName:ShopCartDao <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2017年12月28日 下午12:18:11 <br/> 
 * @author   Pan 
 * @version   
 * @since    JDK 1.8 
 * @see       
 */
public class ShopCartDao extends BaseDao<ShopCart> implements IShopCartDao {

	public ShopCart createEntity() {
		// TODO Auto-generated method stub
		return new ShopCart();
	}

	
	public int saveShopCart(List<ShopCart> li) {
		PreparedStatement ps = null, ps2 = null;
		List<ShopCartDtl> chli ;
		boolean bFlag = false;
		String sql = "Insert into ShopCart(memberid, cartno, shopid, dtlcnt, note) "
				+ "Values(?,?,?,?,?)";
		String sqldtl = "Insert into ShopCartDtl(memberid, cartno, goodsid, qty, spec, price, note, line, total, goodsname) "
				+ "Select ?, ?, ?, ?, ?, ?, ?, ?, ?, g.name From Goods g Where g.id = ?";
		LOGGER.debug("saveShopCart:" + sql);
		LOGGER.debug("saveShopCart:" + sqldtl);
		try {
			ps = getConnection().prepareStatement(sql);
			ps2 = getConnection().prepareStatement(sqldtl);
			for(ShopCart mst : li) {
				chli = new ArrayList<ShopCartDtl>();
				chli.addAll(mst.getLstDtl());
				ps.setInt(1, mst.getMemberid());
				ps.setInt(2, mst.getCartno());
				ps.setInt(3, mst.getShopid());
				ps.setInt(4, mst.getDtlcnt());
				ps.setString(5, mst.getNote());
				ps.addBatch();
				for(ShopCartDtl dtl: chli) {
					ps2.setInt(1, mst.getMemberid());
					ps2.setInt(2, mst.getCartno());
					ps2.setInt(3, dtl.getGoodsid());
					ps2.setInt(4, dtl.getQty());
					ps2.setInt(5, dtl.getSpec());
					ps2.setFloat(6, dtl.getPrice());
					ps2.setString(7, dtl.getNote());
					ps2.setInt(8,  dtl.getLine());
					ps2.setFloat(9, dtl.getTotal());
					ps2.setInt(10, dtl.getGoodsid());
					ps2.addBatch();
					bFlag = true;
				}				
			}
			if (bFlag) {
				ps.executeBatch();
				ps2.executeBatch();
			}

		} catch (SQLException e) {
			LOGGER.error("保存购物车信息出错：" + e.getMessage());
			e.printStackTrace();
			return -1;
		}
		
		return 1;
	}

	public List<ShopCart> readShopCart(int memberid) {
		String sql = "Select * From ShopCart Where memberid = ? Order By Cartno ";
		String sqldtl = "Select * From ShopCartDtl Where memberid = ? Order By Cartno";
		PreparedStatement ps = null, ps2 = null;
		ResultSet rs = null;
		List<ShopCart> li = null;
		int cartno = -1, i=0;
		LOGGER.debug("readShopCart:" + sql);
		LOGGER.debug("readShopCart:" + sqldtl);
		try {
			ps = getConnection().prepareStatement(sql);
			ps2 = getConnection().prepareStatement(sqldtl);
			ps.setInt(1, memberid);
			ps2.setInt(1, memberid);
			li = createEntities(ps.executeQuery());
			if(li!= null) {
				rs = ps2.executeQuery();
				cartno = li.get(0).getCartno();
				li.get(0).setLstDtl(new ArrayList<ShopCartDtl>());
				while(rs != null && rs.next()) {
					if(rs.getInt("cartno") == cartno) {
						ShopCartDtl dtl = new ShopCartDtl();
						dtl.setMemberid(memberid);
						dtl.setCartno(cartno);
						dtl.setGoodsid(rs.getInt("goodsid"));
						dtl.setGoodsname(rs.getString("goodsname"));
						dtl.setPrice(rs.getFloat("price"));
						dtl.setQty(rs.getInt("qty"));
						dtl.setSpec(rs.getInt("spec"));
						dtl.setTotal(rs.getFloat("total"));
						dtl.setNote(rs.getString("note"));
						li.get(i).getLstDtl().add(dtl);
					}else {
						i++;
						cartno = li.get(i).getCartno();
					}
				}
			}
		} catch (SQLException e) {
			LOGGER.error("保存购物车出错：" + e.getMessage());
			e.printStackTrace();
			return null;
		}
		return li;
	}


	public int clearShopCart(int memberid) {
		String sql = "Delete From ShopCart Where memberid = ?";
		String sqldtl = "Delete From ShopCartDtl Where memberid = ?";
		PreparedStatement ps = null;
		LOGGER.debug("clearShopCart:" + sql);
		LOGGER.debug("clearShopCart:" + sqldtl);
		try {
			ps = getConnection().prepareStatement(sql);
			ps.setInt(1, memberid);
			ps.executeUpdate();
			ps = getConnection().prepareStatement(sqldtl);
			ps.setInt(1, memberid);
			ps.executeUpdate();
		} catch (SQLException e) {
			LOGGER.error("清除购物车出错：" + e.getMessage());
			e.printStackTrace();
			return -1;
		}
		
		return 1;
	}

	
	
}
